<template>
  <el-button type="primary" :loading="loading" @click="download">下载箱唛</el-button>
</template>

<script>
import {downloadShipmentBoxLabel} from "@/api/shipmentV2";
import request from "@/utils/request";

export default {
  name: 'DLShipmentBoxBtn',
  props: {
    id: {
      required: true,
    }
  },
  data(){
    return {
      loading: false,
    }
  },
  methods: {
    download(){
      this.loading = true
      request({
        url: `/shipmentV2/downloadShipmentBoxLabel/${this.id}`,
        responseType: "blob", // 需要加上
        isNotJson: true,
        timeout: 60000 * 5, // 1分钟
        handleError: true,
      })
        .then(res=>{
          this.loading = false
          if(res.type === 'application/json'){
            this.$message({
              message: '下载失败，请稍后再尝试',
              duration : 5000,
              type: 'error',
              showClose: true,
            });
            return;
          }

          const pdfFile = window.URL.createObjectURL(
            new Blob([res.data], { type: "application/pdf" })
          );
          // 跳转页面预览
          window.open(pdfFile);
          // 60秒后释放
          setTimeout(()=>{
            URL.revokeObjectURL(pdfFile); //释放URL对象
          },60000)
        })
    },
  }
}
</script>

<style scoped lang="scss">

</style>
